<!DOCTYPE html>
<!--[if IE 8]> 				 <html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->

<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />
    <title>Foundation 4</title>
    <link rel="stylesheet" href="css/foundation.css" />
    <link rel="stylesheet" href="css/custom.css" />
    <link href='http://yandex.st/highlightjs/7.2/styles/vs.min.css' rel='stylesheet' />
    <script src="js/vendor/custom.modernizr.js"></script>
</head>
<body>
    <noscript>
        <style>
            body {
                display: none;
            }
        </style>
    </noscript>
    <div id="loginModal" class="reveal-modal">
        <form onsubmit="return storeUserDetails()">
            <h2>Login</h2>
            <hr/>
            <p class="lead">Please let us know your svn credentials so we can lead you into the awesome!</p>
            <div class="row">
                <div class="large-2 columns">
                    <label for="username" class="right inline">Username</label>
                </div>
                <div class="large-3 columns end">
                    <input id="username" type="text" placeholder="svn-username" required="Required!"/>
                </div>
            </div>
            <div class="row">
                <div class="large-2 columns">
                    <label for="password" class="right inline">Password</label>
                </div>
                <div class="large-3 columns end">
                    <input id="password" type="password" placeholder="svn-password" required="Required!"/>
                </div>
            </div>
            <div class="row">
                <div class="large-offset-2 large-2 end">
                    <input type="submit" value="Submit" class="button"/>
                </div>
            </div>
        </form>
    </div>
    
    <div id="helpModal" class="reveal-modal">
        <h2>Help</h2>
        <hr/>
        <p class="lead">Find answers to your questions below</p>
        <p>Some random text to be replaced by actual help text...</p>
        <a class="close-reveal-modal">&#215;</a>
    </div>

    <div id="header" class="row collapse">
        <div class="large-6 columns">
            <h2>CodeLancer</h2>
        </div>
        <div class="large-6 columns C_align-parent">
            <div class="C_align-child">
                <span data-tooltip class="has-tip" data-reveal-id="helpModal"
                      title="Click here for help on how to use CodeLancer!">
                    Link 1
                </span>
            </div>
        </div>
    </div>
    
    <div id="repository" class="row collapse">
        <div class="large-12">
            Repository URL
        </div>
        <div class="large-12 columns">
            <form onsubmit="return connectToSVN()">
                <div class="panel">
                    <div class="row collapse">
                        <div class="large-3 columns">
                            <span class="prefix">http://svn.nagarro.local:8080/</span>
                        </div>
                        <div class="large-7 columns">
                            <input type="text" id="repositoryURL" required="Required" />
                        </div>
                        <div class="large-2 columns">
                            <input type="submit" value="Connect" class="button postfix"/>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
    
    <div class="row collapse">
        <div class="large-3 columns full-height">
            <div class="panel full-height">
                <label for="changesets">Select a changeset</label>
                <form action="index.html" class="custom">
                    <select id="changesets" class="dropdown" disabled="disabled" onchange="return onChangesetSelected()">
                        <option>Specify repository first</option>
                    </select>
                </form>
                <label for="changes">Select file</label>
                <form action="index.html" class="custom">
                    <select id="changes" class="dropdown" disabled="disabled" onchange="return onChangeSelected()">
                        <option>Specify repository first</option>
                    </select>
                </form>
            </div>
        </div>
       
        <div class="large-9 columns full-height">
            <pre class="panel code">
                <span class="code-header"></span>
                <code id="code">
                </code>
            </pre>
        </div>
    </div>

    <script>
        document.write('<script src=' +
            ('__proto__' in {} ? 'js/vendor/zepto' : 'js/vendor/jquery') +
            '.js><\/script>')
    </script>
    <script src="js/foundation/foundation.js"></script>
    <script src="js/foundation/foundation.tooltips.js"></script>
    <script src="js/foundation/foundation.forms.js"></script>
    <script src="js/foundation/foundation.reveal.js"></script>
    <script src="js/vendor/highlight.min.js"></script>
    <script src="js/internal/constants.js"></script>
    <script type="text/javascript">
        var constants = new Constants();
        
        function storeUserDetails() {
            var username = $('#username').val();
            var password = $('#password').val();

            setLocalStorage(constants.UsernameKey, username);
            setLocalStorage(constants.PasswordKey, password);

            $('#loginModal').foundation('reveal', 'close');
            return false;
        }
        function setLocalStorage(key, value) {
            localStorage[key] = value;
        }
        function getLocalStorage(key) {
            return localStorage[key];
        }
        function checkLogin() {
            if (!getLocalStorage(constants.PasswordKey)) {
                $('#loginModal').foundation('reveal', 'open');
            }
        }
        function forgetPassword() {
            setLocalStorage(constants.PasswordKey, "");
        }

        function getSVNData() {
            var svnURL = constants.SVNBaseURL + $("#repositoryURL").val();
            var username = getLocalStorage(constants.UsernameKey);
            var password = getLocalStorage(constants.PasswordKey);
            var requestData = {
                "userName": username,
                "password": password,
                "path": svnURL
            }
            return requestData;
        }

        function connectToSVN() {
            
            var requestData = getSVNData();
            requestData.size = 50;
            $.ajax({
                url: "http://localhost:3000/GetChangeset",
                type: "POST",
                datatype: 'jsonp',
                data: JSON.stringify(requestData),
                contentType: "application/json",
                success: function (data) {
                    var fetchedChangesets = [];
                    var serverResponse = JSON.parse(data);
                    for (var count = 0; count < serverResponse.logentry.length; count++) {
                        fetchedChangesets.push(
                            {
                                author: serverResponse.logentry[count].author,
                                datetime: serverResponse.logentry[count].date,
                                revision: serverResponse.logentry[count].$.revision
                            }
                            );
                    }
                //    var fetchedChangesets = [
                //{
                //    author: "Raman",
                //    datetime: "20th Oct 2013 10.13 AM",
                //    revision: "123"
                //},
                //{
                //    author: "Pulkit",
                //    datetime: "20th Oct 2013 10.12 AM",
                //    revision: "123"
                //},
                //{
                //    author: "Asit",
                //    datetime: "20th Oct 2013 10.11 AM",
                //    revision: "123"
                //},
                //{
                //    author: "Manish",
                //    datetime: "20th Oct 2013 10.10 AM",
                //    revision: "123"
                //},
                //{
                //    author: "Vibhor",
                //    datetime: "20th Oct 2013 10.09 AM",
                //    revision: "123"
                //},
                //{
                //    author: "Raman",
                //    datetime: "20th Oct 2013 10.08 AM",
                //    revision: "123"
                //}
                //    ];

                    var changesetsDDL = $('#changesets');
                    changesetsDDL.removeAttr('disabled');
                    $('#changesets option').remove();
                    $.each(fetchedChangesets, function (val, text) {
                        changesetsDDL.append($('<option></option>').val(text.revision).html(text.author + " " + text.datetime));
                    });
                    Foundation.libs.forms.refresh_custom_select($('.dropdown'), true);
                },
                error: function (err) {
                    alert('error' + err);
                }
            }
            );
            /* AJAX request to node URL
            */
            

            return false;
        }
		
		function onChangesetSelected(){
		var changesetsDDL = $('#changesets');
		//alert('Changeset:' + changesetsDDL.val()); 
		var requestData = getSVNData();
		requestData.changeset = changesetsDDL.val();

		$.ajax({
		    url: "http://localhost:3000/GetChangesetDetails",
		    type: "POST",
		    datatype: 'jsonp',
		    data: JSON.stringify(requestData),
		    contentType: "application/json",
		    success: function (data) {
		        var fetchedChanges = [];
		        var serverResponse = JSON.parse(data);
		        for (var count = 0; count < serverResponse.logentry.paths.path.length; count++) {
		            fetchedChanges.push(
                                    {
                                        filename: serverResponse.logentry.paths.path[count]._.split('/').pop(),
                                        path: serverResponse.logentry.paths.path[count]._,
                                        type: serverResponse.logentry.paths.path[count].$.kind,
                                        action: serverResponse.logentry.paths.path[count].$.action
                                    }
                            );
		        }
		     
		        var changesDDL = $('#changes');
		        changesDDL.removeAttr('disabled');
		        $('#changes option').remove();
		        $.each(fetchedChanges, function (val, text) {
		            changesDDL.append($('<option></option>').val(text.path).html(text.filename));
		        });
		        Foundation.libs.forms.refresh_custom_select($('.dropdown'), true);
		    },
		    error: function (err) {
		        alert('error' + err);
		    }
		}
        );

		return false;
		}
		
        function onPageLoad() {
            $(".full-height").height($(window).height() - $("#header").height() - $("#repository").height() - 10);
            hljs.initHighlightingOnLoad();
            checkLogin();
            $('#username').val(getLocalStorage(constants.UsernameKey));
            window.onbeforeunload = forgetPassword;
        }
        $(onPageLoad());
    </script>
    <!--
  
        <script src="js/foundation/foundation.js"></script>
  
        <script src="js/foundation/foundation.alerts.js"></script>
  
        <script src="js/foundation/foundation.clearing.js"></script>
  
        <script src="js/foundation/foundation.cookie.js"></script>
  
        <script src="js/foundation/foundation.dropdown.js"></script>
  
  
        <script src="js/foundation/foundation.joyride.js"></script>
  
        <script src="js/foundation/foundation.magellan.js"></script>
  
        <script src="js/foundation/foundation.orbit.js"></script>
  
  
        <script src="js/foundation/foundation.section.js"></script>
  
  
        <script src="js/foundation/foundation.topbar.js"></script>
  
        <script src="js/foundation/foundation.interchange.js"></script>
  
        <script src="js/foundation/foundation.placeholder.js"></script>
  
        <script src="js/foundation/foundation.abide.js"></script>
  
        -->
  
    <script type="text/javascript">
        $(document).foundation();
    </script>
</body>
